package com.singing.manager;

import com.singing.dao.JudgeDao;
import com.singing.model.Command;
import com.singing.model.Judge;

import java.util.List;
import java.util.Scanner;

public class JudgeUpdateManager extends BaseManager {


    private static final int REFRESH_PAGE = 1;
    // 删除裁判
    private static final int UPDATE_JUDGE = 2;

    JudgeDao judgeDao = new JudgeDao();

    public JudgeUpdateManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected int processCommand(int command) {
        switch (command) {
            case REFRESH_PAGE:
                break;
            case UPDATE_JUDGE:
                updateJudge();
                break;
        }
        return command;
    }

    private void updateJudge() {
        Scanner scanner = new Scanner(System.in);
        Integer id = null;
        // 更新裁判信息
        while (true) {
            System.out.println("请输入裁判id");
            String idStr = scanner.next();

            // 判断是否是数字
            try {
                id = Integer.parseInt(idStr);
                break;
            } catch (NumberFormatException e) {
                e.printStackTrace();
                System.out.println("裁判id输入有误，请重新输入");
            }
        }

        System.out.println("请输入裁判名称");
        String name = scanner.next();

        Integer gender = null;
        while (true) {
            System.out.println("裁判性别(0-男，1-女)：");
            String genderStr = scanner.next();
            gender = null;
            try {
                gender = Integer.parseInt(genderStr);

                // 判断性别是否合法
                if (gender != 0 && gender != 1) {
                    throw new NumberFormatException();
                }

                break;

            } catch (NumberFormatException e) {
                e.printStackTrace();
                System.out.println("裁判性别输入错误，请重新输入");
            }
        }

        Judge Judge = new Judge(id, name, gender);

        int result = judgeDao.update(Judge);

        if (result == 1) {
            System.out.println("更新成功");
        } else {
            System.err.println("更新失败");
        }


    }

    @Override
    protected void printTitleExtra() {
        System.out.println("裁判id\t\t裁判名称\t\t裁判性别");
        showJudgeList();
    }

    private void showJudgeList() {
        List<Judge> judges = judgeDao.selectAll();
        for (Judge judge : judges) {
            System.out.println(judge.getId() + "\t\t" + judge.getName() + "\t\t" + (judge.getGender() == 0 ? "男" : "女"));
        }
    }
}
